﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Security;
using System.Data.SqlClient;

using DM_Article.Model;
using DM_Article.Bll;

public partial class admin_admin_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Session["username"] = "";
        }
    }

    private admin getadmin()
    {
        admin adn = new admin();
        adn.username = this.txtusername.Text;
        adn.password = this.txtpassword.Text;
        return adn;
    }

    private bool checkadmintext()
    {
        bool ca = true;
        if (this.txtusername.Text == "")
        {
            Response.Write("<script language='javascript'>alert('用户名不能为空！');</script>");
            this.txtusername.Focus();
            return false;
        }
        if (this.txtpassword.Text == "")
        {
            Response.Write("<script language='javascript'>alert('密码不能为空！');</script>");
            this.txtpassword.Focus();
            return false;
        }
        if (this.txtValidateCode.Text == "")
        {
            Response.Write("<script language='javascript'>alert('验证码不能为空！');</script>");
            this.txtValidateCode.Focus();
            return false;
        }
        return ca;
    }


    protected void btn_ok_Click(object sender, EventArgs e)
    {
        using (SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString()))
        {
            cn.Open();
            if (checkadmintext())
            {
                string CheckCode = this.txtValidateCode.Text;
                if (CheckCode == Session["VNum"].ToString())
                {
                    if (adminbll.checkAdminPassword(cn, this.txtusername.Text).Rows[0][0].ToString() == this.txtpassword.Text)
                    {
                        Response.Write("<script language='javascript'>alert('登录成功！');location.href = 'admin_main.aspx';</script>");
                        Session["username"] = this.txtusername.Text;
                    }
                    else
                    {
                        Response.Write("<script language='javascript'>alert('密码错误！');</script>");
                        this.txtpassword.Text = "";
                        this.txtpassword.Focus();
                    }
                }
                else
                {
                    Response.Write("<script language='javascript'>alert('验证码错误！');</script>");
                    this.txtValidateCode.Text = "";
                    this.txtValidateCode.Focus();
                }
            }    
        }
    }
}
